package cn.ac.istic.lkt.atv;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class FrmAbstract extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5337444691999637810L;
	public static final int S = 0;
	public static final int D = 1;
	protected int mode = 0; // 模式：单树 ，双树
	List<SentenceTree> stlistf;
	List<SentenceTree> stliste = null;
	List<String> stlista = null;

	protected JMenuBar menuBar;
	protected JMenu menuFile;
	// file
	protected JMenuItem menuItemLoad;
	protected JMenuItem menuItemExportSen;
	protected JMenuItem menuItemExportImg;
	protected JMenuItem menuItemExit;	
	protected FileDlg fdlg = null;
	// help
	protected JMenu menuHelp;
	protected JMenuItem aboutItem;
	protected AboutDlg aboutDlg;
	
	SentenceListPanel senPanel;
	
	JScrollPane scrollPane;
	BorderLayout borderLayout1;
	String lastPath = null;

	String ffname = null;
	String efname = null;
	String afname = null;
	boolean balign = false;

	public FrmAbstract(){
		super();
		menuBar = new JMenuBar();
		menuFile = new JMenu();
		menuItemLoad = new JMenuItem();
		menuItemExportSen = new JMenuItem();
		menuItemExportImg = new JMenuItem();
		menuItemExit = new JMenuItem();
		menuHelp = new JMenu();
		aboutItem = new JMenuItem();
		scrollPane = new JScrollPane();
		borderLayout1 = new BorderLayout();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().setLayout(borderLayout1);
		
		this.setJMenuBar(menuBar);

		menuItemLoad.setText("Load Files");		
		menuItemExportSen.setText("Export Sentences");		
		menuItemExportImg.setText("Export Images");
		menuItemExit.setText("Exit");
		
		menuFile.setText("File");
		menuFile.add(menuItemLoad);

		menuFile.add(menuItemExportSen);
		menuFile.add(menuItemExportImg);
		menuFile.addSeparator();
		menuFile.add(menuItemExit);

		menuBar.add(menuFile);

		senPanel = new SentenceListPanel(this);
		getContentPane().add(senPanel, BorderLayout.NORTH);

		scrollPane.setAutoscrolls(true);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		Toolkit tk = this.getToolkit();
		Dimension wndSize = tk.getScreenSize();
		this.setBounds(wndSize.width / 20, wndSize.height / 20,
				wndSize.width * 18 / 20, wndSize.height * 18 / 20);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				performExit();
			}
		});				
	}

	
	public void performExit() {
		System.exit(0);
	}
	
	public void exportSentence(List<SentenceTree> sl) {
		System.out.println(sl.size());
		if (sl == null || sl.size() == 0) {
			JOptionPane.showMessageDialog(this,
					"Please Load Tree file first!", "Error!",
					JOptionPane.WARNING_MESSAGE);
			System.err.println("Trees are not loaded yet!");
			return;
		}
		JFileChooser jfc = new JFileChooser();
		if (lastPath != null) {
			File lastd = new File(lastPath);
			jfc.setCurrentDirectory(lastd);
		}

		jfc.setMultiSelectionEnabled(false);
		int option = jfc.showSaveDialog(this);
		String fname = null;
		if (option == JFileChooser.APPROVE_OPTION) {
			File f = jfc.getSelectedFile();
			fname = jfc.getName(f);
			if (f.isFile())
				fname = f.getName();
			f = jfc.getCurrentDirectory();
			f = new File(f.getPath().concat(File.separator).concat(fname));
			if (f.exists()) {
				int i = javax.swing.JOptionPane.showConfirmDialog(this,
						"该文件已经存在，确定要覆盖吗？");
				if (i == javax.swing.JOptionPane.YES_OPTION)
					;
				else
					return;
			}
			try {
				f.createNewFile();
				java.io.FileWriter fw = new java.io.FileWriter(f);
				for (int i = 0; i < sl.size(); i++) {
					fw.write(sl.get(i).getSents() + "\n");
				}
				fw.close();
				// 向要保存的文件写数据
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {

		}
	}
	
	

	public void drawTree(int id){
		;
	}
	public void readFiles() {
		
	}
	public SentenceListPanel getSenPanel() {
		return senPanel;
	}

	public void setSenPanel(SentenceListPanel senPanel) {
		this.senPanel = senPanel;
	}

	public String getFfname() {
		return ffname;
	}

	public void setFfname(String ffname) {
		this.ffname = ffname;
	}

	public String getEfname() {
		return efname;
	}

	public void setEfname(String efname) {
		this.efname = efname;
	}

	public String getAfname() {
		return afname;
	}

	public void setAfname(String afname) {
		this.afname = afname;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}

	public int getMode() {
		return mode;
	}


	public String getLastPath() {
		return lastPath;
	}

	public void setLastPath(String lastPath) {
		this.lastPath = lastPath;
	}

	public boolean isBalign() {
		return balign;
	}

	public void setBalign(boolean balign) {
		this.balign = balign;
	}
}
